cmake_minimum_required(VERSION 3.1)
project(onenet_tracing C CXX)

# ==============================================================================
# Version information

set(OTTRACER_VERSION_MAJOR "0")
set(OTTRACER_VERSION_MINOR "8")
set(OTTRACER_VERSION_PATCH "1")
set(OTTRACER_VERSION_STRING
        "${OTTRACER_VERSION_MAJOR}.${OTTRACER_VERSION_MINOR}.${OTTRACER_VERSION_PATCH}")

# ==============================================================================
# Set up options

# Introduce variables:
# * CMAKE_INSTALL_LIBDIR
# * CMAKE_INSTALL_BINDIR
# * CMAKE_INSTALL_INCLUDEDIR
include(GNUInstallDirs)

option(BUILD_TESTING "Build test" ON)
option(BUILD_SHARED_LIBS "Build as a shared library" ON)
option(BUILD_STATIC_LIBS "Build as a static library" ON)

if (NOT BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS)
    message(FATAL_ERROR "One or both of BUILD_SHARED_LIBS or BUILD_STATIC_LIBS must be set to ON to build")
endif ()

# ==============================================================================
# Set up generated header files config.h and version.h

configure_file(version.h.in include/ottracer/version.h)
configure_file(config.h.in include/ottracer/config.h)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/include/ottracer
        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

# ==============================================================================
# Configure compiler warnings

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

#set(CMAKE_CXX_COMPILER "g++") #允许使用clang进行编译
set(CMAKE_CXX_FLAGS_DEBUG "-O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -finline-limit=1000 -DNDEBUG")

if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
    set(WARNING_CXX_FLAGS -Weverything
            -Wno-c++98-compat
            -Wno-c++98-compat-bind-to-temporary-copy
            -Wno-weak-vtables
            -Wno-exit-time-destructors
            -Wno-global-constructors
            -Wno-sign-conversion
            -Wno-padded
            -Wno-date-time
            -Wno-switch-enum
            -Wno-disabled-macro-expansion)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
    set(WARNING_CXX_FLAGS
            -g
            -fpic
            -Wall
            -Wextra
            -Werror
            -Wconversion
            -Wno-unused-parameter
            -Wold-style-cast
            -Woverloaded-virtual
            -Wpointer-arith
            -Wshadow
            -Wwrite-strings
            -march=native
            -rdynamic
            -DCHECK_PTHREAD_RETURN_VALUE
            )
endif ()

if (CMAKE_BUILD_BITS EQUAL 32)
    list(APPEND WARNING_CXX_FLAGS "-m32")
endif ()
string(REPLACE ";" " " CMAKE_CXX_FLAGS "${WARNING_CXX_FLAGS}")

#输出最终的编译选项
message(STATUS "CXX_FLAGS = " ${CMAKE_CXX_FLAGS} " " ${CMAKE_CXX_FLAGS_${BUILD_TYPE}})

#添加gcc编译器版本限制
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND
        CMAKE_CXX_COMPILER_VERSION VERSION_LESS "7.3")
    message(FATAL_ERROR "Must use gcc >= 7.3")
endif ()

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

# ==============================================================================
# Find packages

find_package(Protobuf REQUIRED)
#find_package(rapidjson REQUIRED)

if (NOT DEFINED OPENTRACING_INCLUDE_DIR)
    find_path(OPENTRACING_INCLUDE_DIR NAMES opentracing/tracer.h)
endif()
if (NOT DEFINED OPENTRACING_LIBRARY)
    find_library(OPENTRACING_LIBRARY opentracing)
endif()
include_directories(SYSTEM ${OPENTRACING_INCLUDE_DIR})
include_directories(SYSTEM ${PROTOBUF_INCLUDE_DIRS})

set(OTTRACER_LINK_LIBRARIES ${OPENTRACING_LIBRARY} ${PROTOBUF_LIBRARIES})

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
list(APPEND OTTRACER_LINK_LIBRARIES Threads::Threads)

if (BUILD_BENCHMARKING)
    find_package(benchmark REQUIRED)
endif()

# ==============================================================================
# Build OTTracer tracer library

add_subdirectory(3rd_party)
include_directories(SYSTEM ${OTTRACER_THIRD_PARTY_INCLUDES})

include_directories(${CMAKE_CURRENT_LIST_DIR}/include)

set(OTTRACER_SRCS
        src/Tracer.cc
        src/utility.cc)

if (BUILD_SHARED_LIBS)
    add_library(ottracer SHARED
#            $<TARGET_OBJECTS:lightstep_tracer_common>
#            $<TARGET_OBJECTS:lightstep_tracer_configuration>
            $<TARGET_OBJECTS:ottracer_3rd_party>
            ${OTTRACER_SRCS})
    target_compile_options(ottracer PUBLIC ${WARNING_CXX_FLAGS})
    if (ENABLE_LINTING)
        include(OTTracerClangTidy)
        _apply_clang_tidy_if_available(ottracer)
    endif()
    target_link_libraries(ottracer ${OTTRACER_LINK_LIBRARIES})
    set_target_properties(ottracer PROPERTIES SOVERSION ${OTTRACER_VERSION_MAJOR})
    install(TARGETS ottracer LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

include(CTest)
if (BUILD_TESTING AND BUILD_SHARED_LIBS)
    add_subdirectory(test)
#    add_subdirectory(example)
endif()